;
(function() {
	var role = require("./role");
	var BusCon = require("../../core/bus/_con");
	var UserCon = require("../../core/user/_con");
	var GoodsCon = require("../../core/goods/_con");
	var AchCon = require("../../core/ach/_con");
	var db = require("../../core/db/index");
	var config = require("../../core/_include/config");
	var ResponObj = require("../_include/responObj");
	module.exports = {
		prefix: "/bus/goods",
		get: {
			// "/find/:goods_name":require("./goods.rest").get["/one/:goods_name"],
			"/list": require("./goods.rest").get["/list"],
			"/list/quality": require("./goods.rest").get["/list/quality"],
			"/search": require("./goods.rest").get["/search"],
		},
		post: {
			//直接批量加入单种商品
			//.body:goods_info
			//.query.num:goods num||1
			"/": function() {
				var bus = role.getBus(this);
				var query = this.request.query;
				var body = this.request.body;
				var goods_id = bus.importGoods(this.request.body, query.num || body.num || 1, query.no_store || body.no_store);
				this.body = ResponObj("json", GoodsCon.get(goods_id));
			},
			//添加复合的商品
			//.body.goods:mix_goods:[{goods_info,num}...]
			"/mix": function() {
				try {
					var bus = role.getBus(this);
				} catch (errorMsg) {
					this.body = ResponObj("error", errorMsg);
					return;
				}
				var mix_goods = this.request.body.mix_goods;
				if (mix_goods) {
					try {
						mix_goods = JSON.parse(mix_goods);
					} catch (e) {
						this.body = ResponObj("error", "参数解析错误")
					}
					if (!(mix_goods instanceof Array)) {
						this.body = ResponObj("error", "参数错误")
					}
					try {
						mix_goods.forEach(function(goods) {
							bus.importGoods(goods.goods_info, goods.num);
						});
						this.body = ResponObj("json", "添加商品信息成功");
					} catch (e) {
						this.body = ResponObj("error", "参数解析错误")
					}
				} else {
					this.body = ResponObj("error", "参数错误")
				}
			}
		},
		put: {
			"/": function() {
				var bus = role.getBus(this);
				var goods_id = bus.updateGoodsInfo(this.request.body);
				this.body = ResponObj("json", GoodsCon.get(goods_id));
			},
			"/index": function() {
				var bus = role.getBus(this);
				var body = this.request.body;
				var goods_id = body.goods_id;
				var goods_index = body.goods_index;
				if (!goods_id) {
					throw "参数错误"
				}
				bus.updateGoodsIndex(goods_id, goods_index);
				//返回相关的数据
				var return_method = require("./goods.rest").get[body.method_name]
				if (return_method) {
					return_method.apply(this, arguments);
				} else {
					this.body = ResponObj("json", "success");
				}
			},
			//上架
			"/remove_goods_from_un_sold_goods": function() {
				var bus = role.getBus(this);
				var body = this.request.body;
				var goods_id = body.goods_id;
				bus.removeGoodsFrom_UnSoldGoods(goods_id);
				this.body = ResponObj("json", {
					bus: bus,
					goods: GoodsCon.get(goods_id)
				});
			},
			//下架
			"/add_goods_to_un_sold_goods": function() {
				var bus = role.getBus(this);
				var body = this.request.body;
				var goods_id = body.goods_id;
				bus.addGoodsTo_UnSoldGoods(goods_id);
				this.body = ResponObj("json", {
					bus: bus,
					goods: GoodsCon.get(goods_id)
				});
			}
		},
		"delete": {
			"/": function() {
				var bus = role.getBus(this);
				var body = this.request.body;
				var goods_id = body.goods_id;
				var num = body.num;
				var page = body.page;
				if (!goods_id) {
					throw "参数错误"
				}
				bus.removeGoods(goods_id);
				//返回列表
				this.body = ResponObj("json", bus.getGoodsList(num, page, body));
			}
		}
	}
}());